
package com.sailing.lianxi.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.sound.sampled.Line;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class SshTool {

    private static Connection openConnection(String userName,String passWord,String ip,int port) throws Exception {
        Connection conn = new Connection(ip, port);
        conn.connect();
        boolean isAuthed = conn.authenticateWithPassword(userName,passWord);
        if(!isAuthed){
            throw new Exception("Authentication failed");
        }
        return conn;
    }
    
    public static void execShellCmd(String cmd){
        String userName = "root";
        String passWord = "sailing";
        String ip = "172.20.46.211";
        int port = 22;
        InputStream stdout = null; 
        BufferedReader br = null;  
        Connection conn = null;
        Session session =null;
        StringBuffer resultBuff = new StringBuffer("exec result:"); 
        resultBuff.append(System.getProperty("line.separator"));
        try {
            conn = openConnection(userName,passWord,ip,port);
            session = conn.openSession();
            session.execCommand(cmd);
            stdout = new StreamGobbler(session.getStdout());
            InputStream stderr = new StreamGobbler(session.getStderr());
            br = new BufferedReader(new InputStreamReader(stdout)); 
            String line = "";
            while((line = br.readLine()) != null){
                resultBuff.append(line);
                resultBuff.append(System.getProperty("line.separator"));
            }
            System.out.println(resultBuff);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                if(null!=br){
                    br.close();
                }
                if(null!=stdout){
                    stdout.close();
                }
                if(null != session){
                    session.close();
                }
                if(null != conn){
                    conn.close();
                }
            } catch (Exception e2) {
                // TODO: handle exception
            }
           
        }
    }
    
    public static void execScp(String cmd){
        String userName = "root";
        String passWord = "sailing";
        String ip = "172.20.46.211";
        int port = 22;
        InputStream stdout = null; 
        BufferedReader br = null;  
        Connection conn = null;
        Session session =null;
        StringBuffer resultBuff = new StringBuffer("exec result:"); 
        resultBuff.append(System.getProperty("line.separator"));
        try {
            conn = openConnection(userName,passWord,ip,port);
            SCPClient client = new SCPClient(conn);
            stdout = new StreamGobbler(session.getStdout());
            InputStream stderr = new StreamGobbler(session.getStderr());
            br = new BufferedReader(new InputStreamReader(stdout)); 
            String line = "";
            while((line = br.readLine()) != null){
                resultBuff.append(line);
                resultBuff.append(System.getProperty("line.separator"));
            }
            System.out.println(resultBuff);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                if(null!=br){
                    br.close();
                }
                if(null!=stdout){
                    stdout.close();
                }
                if(null != session){
                    session.close();
                }
                if(null != conn){
                    conn.close();
                }
            } catch (Exception e2) {
                // TODO: handle exception
            }
           
        }
    }
    
    public static void main(String[] args) {
        String cmd = "ls /";
        execShellCmd(cmd);
    }
}

